package com.ruoyi.project.mall.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.men.service.MenNeedInfoService;
import com.ruoyi.project.men.service.MenNeedUserService;
import com.ruoyi.project.wx.service.ChinaAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/api/mallNeed")
public class ApiMallNeedController {

    @Autowired
    private MenNeedInfoService menNeedInfoService;
    @Autowired
    private ChinaAreaService chinaAreaService;
    @Autowired
    private MenNeedUserService menNeedUserService;


    /***
     * 删除我得需求
     * @param params  status 0我领取的 1我发布的  status是0 needId是返回的myNeedId 1 是needId
     * @return
     */
    @PostMapping( "/deleteMyNeed")
    @ResponseBody
    public R deleteMyNeed(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("status") || StringUtils.isBlank(params.get("status").toString())
                    ||!params.containsKey("needId") || StringUtils.isBlank(params.get("needId").toString())
            ) {
                return R.error("参数错误");
            }

            return  menNeedUserService.deleteMyNeed(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /***
     * 我领取的需求
     * @param params  status 0:我领取得 1:我发布得
     * @return
     */
    @PostMapping( "/myNeedList")
    @ResponseBody
    public R myNeedList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("status") || StringUtils.isBlank(params.get("status").toString())
            ) {
                return R.error("参数错误");
            }

            return  menNeedUserService.myNeedList(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 领取需求
     * @param params
     * @return
     */
    @PostMapping( "/receiveNeed")
    @ResponseBody
    public R receiveNeed(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("needId") || StringUtils.isBlank(params.get("needId").toString())


            ) {
                return R.error("参数错误");
            }

            return  menNeedUserService.receiveNeed(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /***
     * 实时动态
     * @param
     * @return
     */
    @PostMapping( "/getTodayNeed")
    @ResponseBody
    public R getTodayNeed(){

        try{


            return  menNeedInfoService.getTodayNeed();
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 获取优质需求
     * @param params
     * @return
     */
    @PostMapping( "/getRecommendNeed")
    @ResponseBody
    public R getRecommendNeed(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())


            ) {
                return R.error("参数错误");
            }

            return  menNeedInfoService.getRecommendNeed(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 需求详情
     * @param params needId 需求id userId 用户id
     * @return
     */
    @PostMapping( "/getNeedInfo")
    @ResponseBody
    public R getNeedInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("needId") || StringUtils.isBlank(params.get("needId").toString())
                    ||!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())


            ) {
                return R.error("参数错误");
            }

            return  menNeedInfoService.getNeedInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }




    /**
     * 根据类型和地区获得需求
     * @param params pageNum pageSize userId 没有传-1 typeId,city为空查全部
     * @return
     */
    @PostMapping( "/getNeedListByTypeIdAndCity")
    @ResponseBody
    public R getNeedListByTypeIdAndCity(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("typeId")
                    ||!params.containsKey("city")

            ) {
                return R.error("参数错误");
            }

            return  menNeedInfoService.getNeedListByTypeIdAndCity(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 获取地区/城市
     * @param params pageNum pageSize
     * @return
     */
    @PostMapping( "/getCity")
    @ResponseBody
    public R getCity(@RequestBody Map<String, Object> params){

        try{

            return  chinaAreaService.getCity(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     *  发布需求
     * @param params
     * @return
     */
    @PostMapping( "/saveNeedInfo")
    @ResponseBody
    public R saveNeedInfo(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())
                    ||!params.containsKey("typeId") || StringUtils.isBlank(params.get("typeId").toString())

                    ||!params.containsKey("title") || StringUtils.isBlank(params.get("title").toString())
                    ||!params.containsKey("intro") || StringUtils.isBlank(params.get("intro").toString())
                    ||!params.containsKey("company")
                    ||!params.containsKey("city") || StringUtils.isBlank(params.get("city").toString())
                    ||!params.containsKey("name") || StringUtils.isBlank(params.get("name").toString())
                    ||!params.containsKey("phone") || StringUtils.isBlank(params.get("phone").toString())

            ) {
                return R.error("参数错误");
            }
            return  menNeedInfoService.saveNeedInfo(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     * 最新需求
      * @param params  userId 没有传-1
     * @return
     */
    @PostMapping( "/getNewNeedList")
    @ResponseBody
    public R getNewNeedList(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("userId") || StringUtils.isBlank(params.get("userId").toString())

            ) {
                return R.error("参数错误");
            }
            return  menNeedInfoService.newNeedList(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
}
